//
//  ClassStuDentViewController.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/7/13.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "ClassStuDentViewController.h"
#import "TeacherMessageListViewController.h"
#import "ReportLearnViewController.h"
#import "ClassStidentTableViewCell.h"
#import "StudentModel.h"
#import "TeacherPostGrouRecordViewController.h"

#import "TeaStudentViewController.h"

#import "AlertV.h"

@interface ClassStuDentViewController ()<UITableViewDelegate,UITableViewDataSource,MineAlertVdelegate>
{
    NSInteger page;
}

@property(nonatomic,strong)UITableView *listV;
@property(nonatomic,strong)NSMutableArray *listArr;
@property(nonatomic,strong)AlertV *alertV;

@end

@implementation ClassStuDentViewController

-(NSMutableArray *)listArr
{
    if (!_listArr) {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}

-(UITableView *)listV
{
    if (!_listV) {
        _listV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-20) style:(UITableViewStyleGrouped)];
        _listV.delegate = self;
        _listV.dataSource = self;
        _listV.estimatedSectionHeaderHeight = 0;
        _listV.estimatedSectionFooterHeight = 0;
        _listV.separatorStyle = 0;
        _listV.backgroundColor = [UIColor whiteColor];
        [_listV registerNib:[UINib nibWithNibName:@"ClassStidentTableViewCell" bundle:nil] forCellReuseIdentifier:@"ClassStidentTableViewCell"];
    }
    return _listV;
}

-(AlertV *)alertV
{
    if (!_alertV)
    {
        _alertV = [[AlertV alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-64)];
        _alertV.alpha = 0;
        _alertV.Delegate = self;
    }
    return _alertV;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.listArr.count == 0) {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nodata"]];
    }else
    {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
    }
    return self.listArr.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
/*
 @property (weak, nonatomic) IBOutlet UIImageView *titleImageV;
 @property (weak, nonatomic) IBOutlet UILabel *ClassName;
 @property (weak, nonatomic) IBOutlet UILabel *timeL;
 @property (weak, nonatomic) IBOutlet UILabel *StudentL;
 @property (weak, nonatomic) IBOutlet UILabel *ClassL;
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StudentModel *model = self.listArr[indexPath.row];
    ClassStidentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClassStidentTableViewCell" forIndexPath:indexPath];
    [cell.titiimageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,model.lifethumb]] placeholderImage:nil];
    if ([model.sex isEqualToString:@"男"]) {
        cell.SexImageV.image = [UIImage imageNamed:@"man"];
    }else
    {
        cell.SexImageV.image = [UIImage imageNamed:@"women"];
    }

    cell.numberQingjia.text = model.leave_num;
    cell.titileL.text = model.name;
    cell.detaiL.text = model.birthday;
    cell.timeL.text = [NSString stringWithFormat:@"%@入学",model.adddate];
    cell.selectionStyle = 0;
    return cell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.Name;
    [self.view addSubview:self.listV];
    [self.view addSubview:self.alertV];
    page = 1;
    self.listV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page++;
        [self Request];
    }];
  self.listV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
      [self doRefreshSenior];
  }];
    [self.listV.mj_header beginRefreshing];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.alertV.model = self.listArr[indexPath.row];
    [self.alertV Apper];
}

-(void)doRefreshSenior
{
    page = 1;
    [self Request];
}


-(void)Request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=TeacherCourse&a=coruseStudent" parmDic:@{@"cid":self.cid,@"page":[NSString stringWithFormat:@"%ld",page]} requestType:RequestGET Success:^(id data)
    {
        NSArray *Arr = [StudentModel modelConfigure:data];
        if (page == 1) {
            [self.listArr removeAllObjects];
        }
        if (Arr.count==0) {
            page--;
        }
        [self.listArr addObjectsFromArray:Arr];
        [self.listV reloadData];
        [self.listV.mj_footer endRefreshing];
        [self.listV.mj_header endRefreshing];
        if (Arr.count == 0||Arr.count<10)
        {
            self.listV.mj_footer.state = MJRefreshStateNoMoreData;
        }
    } error:^(NSError *error)
    {

    }];
}

-(void)AlertDidSelect:(NSInteger )index
{
    if (index == 0)
    {
        // 留言
        TeacherMessageListViewController *VC = [[TeacherMessageListViewController alloc]init];
        VC.caid = self.alertV.model.aid;
        VC.sid = self.alertV.model.sid;
        [self.navigationController pushViewController:VC animated:YES];
    }else if(index == 1)
    {
        // 学习小结
        ReportLearnViewController *VC = [[ReportLearnViewController alloc]init];
        VC.model = self.alertV.model;
        [self.navigationController pushViewController:VC animated:YES];
    }else if (index == 2)
    {
        //成长记录
        TeacherPostGrouRecordViewController *VC = [[TeacherPostGrouRecordViewController alloc]init];
        VC.sid = self.alertV.model.sid;
        [self.navigationController pushViewController:VC animated:YES];
    }else if (index == 3)
    {
        // 学生信息
        TeaStudentViewController *VC = [[TeaStudentViewController alloc]init];
        VC.aid = self.alertV.model.aid;
        [self.navigationController pushViewController:VC animated:YES];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
